<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Hometown extends Frontend_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('hometown_model');
    }

    public function index() {
        
        $data = array(
            'global_data' => $this->global_data
        );
        $data ['list_city'] = $this->geo_model->get_by_parent_id();

        if (!isset($_SESSION['current_hometown']) || !count($_SESSION['current_hometown'])) {
            $str_hometown_ids = implode(',', array($_SESSION['member']['city'],$_SESSION['member']['district'],$_SESSION['member']['ward']));
            $_SESSION['current_hometown'] = $this->hometown_model->get_current_home_town($str_hometown_ids);
        }
        

        $this->load->view('layout/header-hometown', $data);
        $this->load->view('vhometown-index', $data);
        $this->load->view('layout/footer', $data);
    }

    function login() {
        $data = array(
            'title' => $this->config->item('default_title'),
            'meta_description' => $this->config->item('default_meta_description'),
            'arr_common' => array(
                'controller' => $this->router->class,
                'method' => $this->router->method
            ),
            'tpl_file' => 'profile/register-login',
        );
        $data ['province'] = $this->geo_model->get_all_province();
        $this->load->view('layout/master', $data);
    }

    function ajax_login() {
        $email = trim($this->input->post('email'));
        $password = trim($this->input->post('password'));
        $remember = intval($this->input->post('remember'));
        $password_md5 = md5($password);
        $remember_expire = 60 * 60 * 24 * 7; //7 ngay

        $arr_profile = $this->profile_model->check_login($email, $password_md5);
        if ($arr_profile) {
            $_SESSION['member'] = $arr_profile;
            if ($remember) {
                $cookies_mail = encrypt($email);
                $cookies_pass = encrypt($password_md5);
                $str_cookies = json_encode(array('email' => $cookies_mail, 'password' => $cookies_pass, 'expire' => encrypt('#####' . (time() + $this->remember_expire) . '#####')));
                setcookie('remember_login', $str_cookies, time() + $remember_expire, '/');
            }
            echo json_encode(array('status' => 'success'));
        } else {
            echo json_encode(array('status' => 'error'));
        }
    }
    
    function ajax_get_district(){
        $pid = intval($this->input->post('pid'));
        if($pid){
            $arr_district = $this->geo_model->get_geo_by_parent_id($pid);
            $html = '<option value="-1">Quận / Huyện...</option>';
            foreach($arr_district as $dict){
                $html = $html . '<option value="'.$dict['id'].'">'.$dict['name'].'</option>';
            }
            echo json_encode(array('status' => 'success','html'=>$html));
        }
    }

    function ajax_register() {

        $fullname = trim($this->input->post('fullname'));
        $email = trim($this->input->post('email'));
        $password = trim($this->input->post('password'));
        $repassword = trim($this->input->post('repassword'));
        $security_code = trim($this->input->post('security_code'));
        $position = trim($this->input->post('position'));
        $geo_id = intval($this->input->post('geo_id'));
        $password_md5 = md5($password);

        if ($geo_id<=0) {
            echo json_encode(
                    array(
                        'status' => 'error',
                        'error_message' => 'Bạn chưa chọn quận huyện'
                    )
            );
            die;
        }
        if ($fullname == '') {
            echo json_encode(
                    array(
                        'status' => 'error',
                        'error_message' => 'Bạn chưa nhập họ và tên',
                        'focus' => '#reg_fullname'
                    )
            );
            die;
        }
        if (!@eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
            echo json_encode(
                    array(
                        'status' => 'error',
                        'error_message' => 'Bạn nhập email không đúng',
                        'focus' => '#reg_email'
                    )
            );
            die;
        }
        if (strlen($password) < 6) {
            echo json_encode(array(
                'status' => 'error',
                'error_message' => 'Bạn nhập mật khẩu quá ngắn',
                'focus' => '#reg_password'
                    )
            );
            die;
        }
        if ($password != $repassword) {
            echo json_encode(
                    array(
                        'status' => 'error',
                        'error_message' => 'Bạn nhập mật khẩu không trùng nhau',
                        'focus' => '#reg_repassword'
                    )
            );
            die;
        }
        if ($position == '' || $position == 0) {
            echo json_encode(
                    array(
                        'status' => 'error',
                        'error_message' => 'Bạn chưa xác định vị trí trên bản đồ'
                    )
            );
            die;
        }
        if ($security_code=='') {
            echo json_encode(
                    array(
                        'status' => 'error',
                        'error_message' => 'Bạn chưa nhập mã bảo mật',
                        'focus'=>'#reg_security_code'
                    )
            );
            die;
        }
        if (strtolower($security_code) != strtolower($_SESSION['captcha_code'])) {
            echo json_encode(
                    array(
                        'status' => 'error',
                        'error_message' => 'Bạn nhập mã bảo mật không đúng',
                        'focus'=>'#reg_security_code'
                    )
            );
            die;
        }

        $arr_insert = array(
            'fullname' => $fullname,
            'email' => $email,
            'password' => $password_md5,
            'map_latlon' => $position,
            'geo_id' => $geo_id,
            'create_date' => date('Y-m-d H:i:s')
        );
        $member_id = $this->profile_model->ajax_register($arr_insert);
        if ($member_id) {
            echo json_encode(array('status' => 'success'));
        }
    }

}
